package com.lottery.controller;

import com.lottery.dto.SpecialNumbersOutputDTO;
import com.lottery.pojo.TableSpecialNumbers;
import com.lottery.service.SpecialNumberService;
import com.lottery.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class SpecialNumberController {

    @Autowired
    private SpecialNumberService specialNumberService;

    @GetMapping("getSpecialNumbers")
    public SpecialNumbersOutputDTO getSpecialNumbers() {
        SpecialNumbersOutputDTO outputDTO = new SpecialNumbersOutputDTO();

        List<TableSpecialNumbers> specialNumbers = specialNumberService.getSpecialNumbers();

        outputDTO.setKNumbers(specialNumbers.stream().filter(tableSpecialNumbers -> tableSpecialNumbers.getRegion_id() == 1).collect(Collectors.toList()));
        outputDTO.setFNumbers(specialNumbers.stream().filter(tableSpecialNumbers -> tableSpecialNumbers.getRegion_id() == 2).collect(Collectors.toList()));
        outputDTO.setDNumbers(specialNumbers.stream().filter(tableSpecialNumbers -> tableSpecialNumbers.getRegion_id() == 3).collect(Collectors.toList()));
        outputDTO.setSNumbers(specialNumbers.stream().filter(tableSpecialNumbers -> tableSpecialNumbers.getRegion_id() == 4).collect(Collectors.toList()));

        return outputDTO;
    }

    @PostMapping("deleteSpecialNumber")
    public void deleteSpecialNumber(long id) {
        specialNumberService.deleteSpecialNumber(id);
    }

    @PostMapping("addSpecialNumber")
    public void addSpecialNumber(int region_id, String number, int limit) {
        TableSpecialNumbers specialNumbers = new TableSpecialNumbers();
        specialNumbers.setRegion_id(region_id);
        specialNumbers.setNumber(number);
        specialNumbers.setLimit(limit);
        String dt = DateUtils.getSub4DT();
        specialNumbers.setCreated_at(dt);
        specialNumbers.setUpdated_at(dt);
        log.info("addSpecialNumber(), specialNumber:" + specialNumbers.toString());
        specialNumberService.addSpecialNumber(specialNumbers);
    }
}
